package Bidostar_Page;

import Bidostar_BaseData.Page;
import Bidostar_Data.Baseinfo;
import Bidostar_Data.Bidostar_GetCode;
import com.csvreader.CsvReader;
import org.openqa.selenium.WebDriver;

import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

/*
    By Dreamslians 20180226
 */
public class LoginPage extends Page
{
    public LoginPage(WebDriver driver)
    {
        super(driver);
    }

    //输入用户名
    public void InputUsername(String username)
    {
        getElement("username").clear();
        getElement("username").sendKeys(username);
    }

    //输入密码
    public void InputPassword(String password)
    {
        getElement("password").clear();
        getElement("password").sendKeys(password);
    }

    //点击记住用户名
    public void clickrememberPW()
    {
        getElement("rememberPW").click();
    }

    //点击登录按钮
    public void clickLoginBtn()
    {
        getElement("LoginBtn").click();
    }

    //点击退出登录按钮
    public void clickExit()
    {
        getElement("Exit").click();
    }


    public boolean PublicLogin()
    {
        try
        {
            if (driver.getCurrentUrl().contains("ci.bidostar.com"))
            {
                InputUsername(Baseinfo.Ci_User);
                InputPassword(Bidostar_GetCode.Ci_Password());
                clickrememberPW();
                clickLoginBtn();
                Thread.sleep(20);
                if (Bidostar_GetCode.Getresult_Insurance().matches("0"))
                {
                    logger.info("保险平台登录成功");
                    return true;
                } else if (Bidostar_GetCode.Getresult_Insurance().matches("2"))
                {
                    logger.warn("用户不存在!");
                    ScreenGet();
                    return false;
                } else if (Bidostar_GetCode.Getresult_Insurance().matches("9"))
                {
                    logger.warn("密码错误!");
                    ScreenGet();
                    return false;
                } else
                {
                    logger.warn("保险平台登录失败");
                    ScreenGet();
                    return false;
                }
            } else if (driver.getCurrentUrl().contains("ta.bidostar.com"))
            {
                InputUsername(Baseinfo.Ta_User);
                InputPassword(Bidostar_GetCode.Ta_Password());
                clickrememberPW();
                clickLoginBtn();
                Thread.sleep(20);
                if (Bidostar_GetCode.Getresult_Traffic().matches("0"))
                {
                    logger.info("交警平台登录成功");
                    return true;
                } else
                {
                    logger.warn("交警平台登录失败");
                    ScreenGet();
                    return false;
                }

            } else if (driver.getCurrentUrl().contains("dc.bidostar.com"))
            {
                InputUsername(Baseinfo.Dc_User);
                InputPassword(Bidostar_GetCode.Dc_Password());
                clickrememberPW();
                clickLoginBtn();
                Thread.sleep(20);

                if (Bidostar_GetCode.Getresult_Operatin().matches("0"))
                {
                    logger.info("调度平台登录成功");
                    return true;
                } else
                {
                    ScreenGet();
                    logger.warn("调度平台登录失败");
                    return false;
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }

    public boolean LoginBySecond()
    {
        try
        {
            if (driver.getCurrentUrl().contains("ta.bidostar.com"))
            {
                logger.info("交警平台");
                InputUsername(Baseinfo.Ta_User);
                InputPassword(Bidostar_GetCode.Ta_Password());
                clickrememberPW();
                clickLoginBtn();
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                if (!driver.getCurrentUrl().contains("login.html"))
                {
                    logger.info("登录成功");
                    logger.info("交警平台正确的用户名是：" + Baseinfo.Ta_User + "密码是：" + Bidostar_GetCode.Ta_Password());
                } else if (getElement("Errormessage").isDisplayed())
                {
                    logger.warn("错误提示：" + getElement("Errormessage").getText());

                    Login();
                } else
                {
                    logger.warn("登录失败");
                    ScreenGet();
                    return false;
                }
            } else if (driver.getCurrentUrl().contains("ci.bidostar.com"))
            {
                logger.info("保险平台");
                InputUsername(Baseinfo.Ci_User);
                InputPassword(Bidostar_GetCode.Ci_Password());
                clickrememberPW();
                clickLoginBtn();
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                if (!driver.getCurrentUrl().contains("login.html"))
                {
                    logger.info("登录成功");
                    logger.info("保险平台正确的用户名是：" + Baseinfo.Ci_User + "密码是：" + Bidostar_GetCode.Ci_Password());
                } else
                {
                    logger.warn("登录失败");
                    ScreenGet();
                    return false;
                }
            } else if (driver.getCurrentUrl().contains("dc.bidostar.com"))
            {
                logger.info("调度平台");
                InputUsername(Baseinfo.Dc_User);
                InputPassword(Bidostar_GetCode.Dc_Password());
                clickrememberPW();
                clickLoginBtn();
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                if (!driver.getCurrentUrl().contains("login.html"))
                {
                    logger.info("登录成功");
                    logger.info("调度平台正确的用户名是：" + Baseinfo.Dc_User + "密码是：" + Bidostar_GetCode.Dc_Password());
                } else
                {
                    logger.warn("登录失败");
                    ScreenGet();
                    return false;
                }
            }
        } catch (Exception e)
        {
            logger.warn("失败");
        }
        return true;

    }

    public boolean Login() throws Exception
    {
        logger.info("项目平台：" + getElement("Company").getText() + "_" + getElement("ProjectName").getText() + getElement("Version").getText());

        String filePath = "src/main/java/Bidostar_Data/demo.csv";
        CsvReader reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
        reader.readHeaders();
        try
        {
            data();
            while (reader.readRecord())
            {
                InputUsername(reader.get("name"));
                logger.info("用户名：" + reader.get("name"));
                InputPassword(reader.get("password"));
                logger.info("密码：" + reader.get("password"));
                clickrememberPW();
                clickLoginBtn();
                Thread.sleep(30);
                if (driver.getCurrentUrl().contains("ta.bidostar.com") && driver.getCurrentUrl().contains("remoteduty.html"))
                {
                    logger.info("登录成功");
                    return true;
                } else if (driver.getCurrentUrl().contains("ci.bidostar.com") && driver.getCurrentUrl().contains("app.html"))
                {
                    logger.info("登录成功");
                    return true;
                } else if (driver.getCurrentUrl().contains("dc.bidostar.com") && driver.getCurrentUrl().contains("app.html"))
                {
                    logger.info("登录成功");
                    return true;
                } else
                {
                    logger.warn("错误提示：" + getElement("Errormessage").getText());
                    ScreenGet();
                    return false;
                }
            }
        } catch (Exception e)
        {
            logger.warn("case执行失败");
            ScreenGet();
        }
        return false;
    }
}